package com.ayoka.ui.assignment;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.ayoka.R;
import com.ayoka.base.AppContext;
import com.ayoka.base.BaseFragment;
import com.ayoka.bean.CourseAssignmentBean;
import com.ayoka.databinding.FragmentCourseAssignmentTrainingBinding;
import com.ayoka.mvp.CourseAssignmentMVP.CourseAssignmentPresenter;
import com.ayoka.mvp.CourseAssignmentMVP.ICourseAssignmentView;
import com.jincp.image.load.ImageLoaderProxy;
import com.jzvd.JZMediaIjkplayer;
import com.jzvd.SimpleJZMediaPlayerListener;

import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerStandard;

public class CourseAssignmentTrainingFragment extends BaseFragment<FragmentCourseAssignmentTrainingBinding, CourseAssignmentPresenter> implements ICourseAssignmentView {


    public static CourseAssignmentTrainingFragment newInstance(CourseAssignmentBean courseAssignmentBean) {
        CourseAssignmentTrainingFragment assignmentFragment = new CourseAssignmentTrainingFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("CourseAssignmentBean", courseAssignmentBean);
        assignmentFragment.setArguments(bundle);
        return assignmentFragment;
    }

    protected CourseAssignmentBean courseAssignmentBean;

    @Override
    protected void configBundleArguments() {
        courseAssignmentBean = (CourseAssignmentBean) getArguments().getSerializable("CourseAssignmentBean");
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_course_assignment_training;
    }


    @Override
    protected void initViews(View view) {
    }

    @Override
    public void initData() {
        // 配置视频
        String videoUrl = "https://media.w3.org/2010/05/sintel/trailer.mp4";
        String thumbImageUrl = "http://img3.imgtn.bdimg.com/it/u=1607825827,433591088&fm=26&gp=0.jpg";
        // 是否已缓存
        boolean isCached = AppContext.proxy.isCached(videoUrl);
        Log.d(TAG, String.valueOf(isCached));
        // 设置视频地址
        mDataBinding.videoPlayer.setUp(videoUrl, JZVideoPlayerStandard.SCREEN_WINDOW_NORMAL, courseAssignmentBean.Title);
        // 增加封面
        ImageLoaderProxy.getInstance().displayImage(thumbImageUrl, mDataBinding.videoPlayer.thumbImageView);
        JZVideoPlayer.setMediaInterface(new JZMediaIjkplayer());
        mDataBinding.videoPlayer.setJZMediaPlayerListener(new SimpleJZMediaPlayerListener(){
            @Override
            public void onStateAutoComplete() {
                Log.d(TAG, "onAutoComplete");
            }
        });

        mDataBinding.tvTrainingContent.setText(courseAssignmentBean.RelaxingExercise.TxtContent);
    }

    @Override
    protected CourseAssignmentPresenter getPresenter() {
        return null;
    }

    @Override
    public void configCourseAssignmentDetailData(CourseAssignmentBean result) {

    }

    @Override
    public void onPause() {
        super.onPause();
        JZVideoPlayer.goOnPlayOnPause();
    }

    @Override
    public void onDestroy() {
        // 释放当前播放器
        mDataBinding.videoPlayer.setJZMediaPlayerListener(null);
        // 释放所有
        JZVideoPlayer.releaseAllVideos();
        super.onDestroy();
    }
}
